/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomerSelectView.java
 *
 * Created on Apr 1, 2011, 11:49:07 PM
 */

package views;

import controllers.PersonController;
import java.util.ArrayList;
import javax.swing.DefaultListModel;
import models.Person;

/**
 *
 * @author pelle
 */
public class PersonSelectView extends AbstractView {

    private PersonController personCtr = new PersonController();
    private ArrayList<Person> searchPersons = new ArrayList<Person>();

    private Object opener = null;
    
    /** Creates new form CustomerSelectView */
    public PersonSelectView(Object opener) {
        initComponents();
        this.opener = opener;
    }

    public PersonSelectView() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        searchBtn = new javax.swing.JButton();
        phone = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        checkMember = new javax.swing.JCheckBox();
        checkSponsor = new javax.swing.JCheckBox();
        checkInspector = new javax.swing.JCheckBox();
        checkTrainer = new javax.swing.JCheckBox();
        checkManager = new javax.swing.JCheckBox();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        customerList = new javax.swing.JList();
        selectPerson = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Søg efter"));

        jLabel2.setText("Navn:");

        name.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                nameKeyReleased(evt);
            }
        });

        searchBtn.setText("Søg");
        searchBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                searchBtnActionPerformed(evt);
            }
        });

        phone.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                phoneKeyReleased(evt);
            }
        });

        jLabel4.setText("Telefon nr.:");

        email.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                emailKeyReleased(evt);
            }
        });

        jLabel5.setText("E-mail:");

        checkMember.setText("Medlem");

        checkSponsor.setText("Sponsor");

        checkInspector.setText("Inspektør");

        checkTrainer.setText("Træner");

        checkManager.setText("Bestyrelse");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel4)
                    .add(jLabel5)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, phone, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, name, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, email, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 115, Short.MAX_VALUE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(checkManager)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(checkTrainer)
                            .add(checkMember))
                        .add(18, 18, 18)
                        .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(checkSponsor)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                .add(searchBtn)
                                .add(checkInspector)))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(jLabel2)
                            .add(23, 23, 23)
                            .add(jLabel4)
                            .add(20, 20, 20)
                            .add(jLabel5))
                        .add(jPanel1Layout.createSequentialGroup()
                            .add(name, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(11, 11, 11)
                            .add(phone, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(8, 8, 8)
                            .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                                .add(email, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(searchBtn))))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(checkMember)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkTrainer)
                        .add(2, 2, 2)
                        .add(checkManager))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(checkSponsor)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(checkInspector)))
                .add(32, 32, 32))
        );

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 24));
        jLabel3.setText("Find person");

        customerList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        customerList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                customerListMouseReleased(evt);
            }
        });
        jScrollPane1.setViewportView(customerList);

        selectPerson.setText("Vælg person");
        selectPerson.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectPersonActionPerformed(evt);
            }
        });

        jLabel1.setText("Vælg personen:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jLabel1)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 455, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, selectPerson))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(7, 7, 7)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 122, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(selectPerson)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void sendPerson() {
        Person person = null;
        boolean success = false;

        int selected = customerList.getSelectedIndex();
        if(selected >= 0) {
            person = searchPersons.get(selected);
        }
        //Class.forName(Class.class.getName()).cast(opener).setCustomer();
        
        if(opener instanceof PersonEditView) {
            PersonEditView view = (PersonEditView)this.opener;
            success = view.addRelatedPerson(person);
        }

        if(success) {
            dispose();
        }
    }
    
    private void selectPersonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectPersonActionPerformed
        this.sendPerson();
    }//GEN-LAST:event_selectPersonActionPerformed

    private void nameKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nameKeyReleased
        if(evt.getKeyCode() == 10) {
            this.searchPersons();
        }
    }//GEN-LAST:event_nameKeyReleased

    private void searchBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_searchBtnActionPerformed
        this.searchPersons();
    }//GEN-LAST:event_searchBtnActionPerformed

    private void phoneKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_phoneKeyReleased
        if(evt.getKeyCode() == 10) {
            this.searchPersons();
        }
    }//GEN-LAST:event_phoneKeyReleased

    private void emailKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_emailKeyReleased
        if(evt.getKeyCode() == 10) {
            this.searchPersons();
        }
    }//GEN-LAST:event_emailKeyReleased

    private void customerListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_customerListMouseReleased
        if(evt.getClickCount() == 2) {
            this.sendPerson();
        }
    }//GEN-LAST:event_customerListMouseReleased

    private void searchPersons() {
        String personName = this.name.getText();
        String personEmail = this.email.getText();
        String personPhone = this.phone.getText();
        ArrayList<String> roles = new ArrayList<String>();
        
        if(checkMember.isSelected()) {
            roles.add("member");
        }
        
        if(checkTrainer.isSelected()) {
            roles.add("trainer");
        }
        
        if(checkManager.isSelected()) {
            roles.add("manager");
        }
        
        if(checkInspector.isSelected()) {
            roles.add("inspector");
        }
        
        if(checkSponsor.isSelected()) {
            roles.add("sponsor");
        }

        this.searchPersons = this.personCtr.searchPersons(personName, personEmail, personPhone, roles);
        this.updateList();
    }

    private void updateList() {
        DefaultListModel listModel = new DefaultListModel();

        if(searchPersons.size() > 0) {
            if(searchPersons != null) {
                for(Person p : searchPersons) {
                    listModel.addElement("#" + p.getId() + ": " + p.getName());
                }
            }
        }

        customerList.removeAll();
        customerList.setModel(listModel);
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PersonSelectView().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JCheckBox checkInspector;
    private javax.swing.JCheckBox checkManager;
    private javax.swing.JCheckBox checkMember;
    private javax.swing.JCheckBox checkSponsor;
    private javax.swing.JCheckBox checkTrainer;
    private javax.swing.JList customerList;
    private javax.swing.JTextField email;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField name;
    private javax.swing.JTextField phone;
    private javax.swing.JButton searchBtn;
    private javax.swing.JButton selectPerson;
    // End of variables declaration//GEN-END:variables

}
